class TeamController {    
  def index = { redirect(action:list,params:params) }
  static allowedMethods = [delete:'POST', save:'POST', update:'POST']

  def list = {
    params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
    [ teamInstanceList: Team.list( params ), teamInstanceTotal: Team.count() ]
  }

  def show = {
    def teamInstance = Team.get(params.id)
    if(!teamInstance) {
      flash.message = "Team not found with id ${params.id}"
      redirect(action:list)
    }
    else { return [ teamInstance : teamInstance ] }
  }

  def delete = {
    def teamInstance = Team.get( params.id )
    if(teamInstance) {
      try {
	teamInstance.delete(flush:true)
	flash.message = "Team ${params.id} deleted"
	redirect(action:list)
      }
      catch(org.springframework.dao.DataIntegrityViolationException e) {
	flash.message = "Team ${params.id} could not be deleted"
	redirect(action:show,id:params.id)
      }
    }
    else {
      flash.message = "Team not found with id ${params.id}"
      redirect(action:list)
    }
  }

  def edit = {
    def teamInstance = Team.get(params.id)
    if(!teamInstance) {
      flash.message = "Team not found with id ${params.id}"
      redirect(action:list)
    }
    else {
      return [ teamInstance : teamInstance ]
    }
  }

  def update = {
    def teamInstance = Team.get(params.id)
    if(teamInstance) {
      if(params.version) {
	def version = params.version.toLong()
	if(teamInstance.version > version) {
	  teamInstance.errors.rejectValue(
	    "version",
	    "team.optimistic.locking.failure",
	    "Another user has updated this Team while you were editing.")
	  render(view:'edit',model:[teamInstance:teamInstance])
	  return
	}
      }
      teamInstance.properties = params
      if(!teamInstance.hasErrors() && teamInstance.save()) {
	flash.message = "Team ${params.id} updated"
	redirect(action:show,id:teamInstance.id)
      }
      else {
	render(view:'edit',model:[teamInstance:teamInstance])
      }
    }
    else {
      flash.message = "Team not found with id ${params.id}"
      redirect(action:list)
    }
  }

  def create = { return ['teamInstance':new Team(params)] }

  def save = {
    def teamInstance = new Team(params)
    if(!teamInstance.hasErrors() && teamInstance.save()) {
      flash.message = "Team ${teamInstance.id} created"
      redirect(action:show,id:teamInstance.id)
    }
    else {
      render(view:'create',model:[teamInstance:teamInstance])
    }
  }

  def membersList = {
    def team = Team.get(params.id.toLong())
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    return [personInstanceList : team.members, 
	    personInstanceTotal : team.members.count(), 
	    teamInstance : team]
  }

  def addMembers = {
    def team = Team.get(params.id.toLong())
    def members = team.members.size() ? Person.createCriteria().list {
      not{ 'in'('id', team.members.inject([]){ l, m -> l.add(m.id); return l}) }
    } : Person.getAll()
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    return [personInstanceList : members, 
	    personInstanceTotal : members.count(), 
	    teamInstance : team]
  }
  
  def saveMembers = {
    def team = Team.get(params.teamId.toLong())
    if (team){
      def personIDs = params.keySet().findAll{ it =~ /^person.(\d+)$/ }.inject([]) { 
	l, i -> l += i.tokenize('-')[1].toLong(); l
      }
      if (!personIDs.size()){
	flash.message = "Nothing has changed"
	redirect(action:show, id: team.id, teamInstance: team)
      }
      else {
	team.members.addAll(Person.createCriteria().list { 'in'('id', personIDs) })
	team.save()
	flash.message = "New members added to ${team.name} team"
	redirect(action:show, id: team.id, teamInstance: team)
      }
    }
    else {
      render(view:'addMembers', model:['teamInstance': teamInstance])
    }
  }
}
